package com.listeningframework.boot.commons.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class DateFormatUtils {
    /**
     * 日志
     */
    private static final Logger LOG = LoggerFactory.getLogger(DateFormatUtils.class);
    /**
     * Listening formatter for date-time without time zone.
     * The format used is <tt>yyyy-MM-dd HH:mm:ss</tt>.
     */
    public static final FastDateFormat TIME_FORMAT = FastDateFormat.getInstance("HH:mm:ss");
    /**
     * Listening formatter for date-time without time zone.
     * The format used is <tt>yyyy-MM-dd</tt>.
     */
    public static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");
    /**
     * Listening formatter for date-time without time zone.
     * The format used is <tt>yyyy-MM-dd HH:mm:ss</tt>.
     */
    public static final FastDateFormat DATETIME_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
    /**
     * Listening formatter for date-time without time zone.
     * The format used is <tt>yyyy-MM-dd HH:mm:ss</tt>.
     */
    public static final FastDateFormat FULL_DATETIME_FORMAT = FastDateFormat.getInstance("yyyyMMddHHmmss");

    /**
     * 当前时间
     *
     * @return Date
     */
    public static Date currentTimeStamp() {
        return new Date();
    }

    /**
     * Format Date Time
     *
     * @param date 日期
     * @return String
     */
    public static String datetime2string(Date date) {
        return date == null ? null : DATETIME_FORMAT.format(date);
    }

    /**
     * Format Date Time
     *
     * @param date   日期
     * @param format 日期格式
     * @return String
     */
    public static String date2string(Date date, FastDateFormat format) {
        return date == null ? null : format.format(date);
    }

    /**
     * Format Date Time
     *
     * @param date 日期
     * @return String
     */
    public static String date2string(Date date) {
        return date == null ? null : DATE_FORMAT.format(date);
    }

    /**
     * Format Date Time
     *
     * @param date 日期
     * @return String
     */
    public static String date2fullstring(Date date) {
        return date == null ? null : FULL_DATETIME_FORMAT.format(date);
    }

    /**
     * String to Date
     *
     * @param value 字符串日期
     * @return Date
     */
    public static Date string2date(String value) {
        return string2date(value, DATE_FORMAT.getPattern());
    }

    /**
     * String to Date
     *
     * @param value  字符串日期
     * @param format 格式
     * @return Date
     */
    public static Date string2date(String value, String format) {
        if (StringUtils.isAnyBlank(value, format)) {
            return null;
        }
        try {
            return FastDateFormat.getInstance(format).parse(value);
        } catch (ParseException e) {
            LOG.error("parse date format fail", e);
            return null;
        }
    }

    /**
     * 时间加减（分钟）
     *
     * @param date  开始日期
     * @param num   增加数
     * @param field 增加单位
     * @return Date
     */
    public static Date add(Date date, int num, int field) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, num);
        return calendar.getTime();
    }
}
